/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author alb3rto
 */
package Modelo.DAL;

import java.sql.SQLException;
import java.sql.Statement;
import Modelo.Cargo;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import org.json.JSONArray;
import org.json.JSONObject;

public class Cargo_DAO {

    public static boolean insertarCargo(Cargo c) {

        String query = "INSERT INTO cargo( nombre_cargo, descripcion) VALUES ( ?, ?)";

        ConexionBD conexion = new ConexionBD();
        PreparedStatement s = null;
        try {
            s = conexion.session.prepareStatement(query);
            s.setString(1, c.getNombreCargo());
            s.setString(2, c.getDescripcion());
            s.executeUpdate();
            return true;
        } catch (SQLException sqle) {
            System.out.println("Error Insertando el Cargo! " + sqle.getMessage());
            return false;
        }
    }

    public static JSONObject consultarCargo(Cargo c, String limit, String offset, String order, String asc) {
        ConexionBD conexion = new ConexionBD();
        Statement s = null;
        ResultSet rs = null;
        JSONObject cargos = new JSONObject();
        try {
            s = conexion.session.createStatement();
        } catch (SQLException se) {
            System.out.println("¡Error Consultando Cargos!");
            return null;
        }

        String query = "SELECT * from cargo ";
        String params = "WHERE (1=1) ";
        if (c.getNombreCargo() != null && !c.getNombreCargo().equals("")) {
            query += "and nombre_cargo LIKE '%" + c.getNombreCargo() + "%'";
        }

        String format = "";
        if (order != null && !order.equals("")) {
            format += " order by " + order;
            if (asc != null && !asc.equals("") && asc.equals("false")) {
                format += " desc";
            } else {
                format += " asc";
            }
        }

        if (limit != null && !limit.equals("")) {
            format += " limit " + limit;
        }
        if (offset != null && !offset.equals("")) {
            format += " offset " + offset;
        }

        try {
            rs = s.executeQuery(query + params + format);
            JSONArray ja = new JSONArray();
            int i = 0;
            while (rs.next()) {
                JSONObject cargo = new JSONObject();
                cargo.put("nombre_cargo", rs.getString(1));
                cargo.put("descripcion", rs.getString(2));
                ja.put(i++, cargo);
            }
            cargos.put("cargos", ja);
            System.out.println(ja.toString());

            query = "select count(*) from cargo " + params;
            rs = s.executeQuery(query);
            if (rs.next()) {
                cargos.put("tam", rs.getString(1));
            }
            return cargos;
        } catch (SQLException se) {
            System.out.println("Error obteniendo Cargos." + se.getMessage());
            return cargos;
        } catch (Exception e) {
            System.out.println("Error obteniendo Cargos." + e.getMessage());
            return cargos;
        }
    }
}
